package service

import (
	"context"

	"gitee.com/WisdomClassroom/core"

	"gitee.com/WisdomClassroom/chapter/glb"
	"gitee.com/WisdomClassroom/core/models"
	"gitee.com/WisdomClassroom/core/protobufs/v1/pb"
	"gorm.io/gorm"
)

func (s *Service) GetChapter(ctx context.Context, request *pb.GetChapterRequest) (*pb.GetChapterResponse, error) {
	chapter := &models.Chapter{UUID: request.GetUUID()}
	err := glb.DB.Take(chapter).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return &pb.GetChapterResponse{Status: &pb.ResponseStatus{
				Code:    core.ResponseStatusCodeOtherError,
				Message: "章节不存在",
			}}, nil
		}
		glb.Logger.Error(err.Error())
		return getChapterRespSvrErr, nil
	}

	return &pb.GetChapterResponse{
		Status:      &pb.ResponseStatus{Code: core.ResponseStatusCodeSuccess},
		UUID:        chapter.UUID,
		Name:        chapter.Name.String,
		Description: chapter.Description.String,
		Index:       chapter.Index.Int32,
		Content:     chapter.Content.String,
	}, nil
}
